<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Image extends CI_Controller {
    
    private $data = array();

    public function  __construct() {
        parent::__construct();

        //check session
        if( !$this->session->userdata('isLoggedIn') ) {
            redirect(base_url().'admin', 'refresh');
        }

        //Load Helper and library
        $this->load->helper("url");
        $this->load->library("ci_smarty");
        $this->load->helper("text"); 
        $this->data['base_url'] = base_url();
        $this->data['root_url'] = $this->config->item('root_url');
        $this->load->library('pagination'); 

        //Load Model
        $this->load->Model("admin/Mimage");

        $this->data['nameAdmin'] = $this->session->userdata('name');

        $this->data['main_selected_cate'] = 'image';
    }

    public function listImage($curPage = 1 )
    {
        if($this->input->post()){
            $image = $this->Mimage->uploadImage();
            if($image['0']['is_image'] == 1){
                $dataLagre = $this->input->post();
                $dataLagre['filename'] = $image['0']['file_name'];
                $dataLagre['width']    = $image['0']['image_width'];
                $dataLagre['height']   = $image['0']['image_height'];
                $dataLagre['type'] = 'L';
                
                $dataMedium = $this->input->post();
                $dataMedium['filename'] = $image['0']['file_name'];
                $dataMedium['width']    = $image['1']['width'];
                $dataMedium['height']   = $image['1']['height'];
                $dataMedium['type']     = 'M';
                
                $dataSmall = $this->input->post();
                $dataSmall['filename'] = $image['0']['file_name'];
                $dataSmall['width']    = $image['2']['width'];
                $dataSmall['height']   = $image['2']['height'];
                $dataSmall['type']     = 'S';
                
                $this->Mimage->insertImage($dataLagre);
                $this->Mimage->insertImage($dataMedium);
                $this->Mimage->insertImage($dataSmall);
                redirect(base_url()."admin/list-image.html");
            }
        }

        // config page
        $categoryPageConfig['base_url']      = base_url("list-image/");
        $categoryPageConfig['total_rows']    = $this->Mimage->countImage();
        $categoryPageConfig['per_page']      = 20;
        $categoryPageConfig['uri_segment']   = 2;
        $categoryPageConfig['cur_page']      = $curPage;

        $curOffset = $categoryPageConfig['per_page'] * ($curPage - 1);

        $this->pagination->initialize($categoryPageConfig);

        $this->data['pageList'] = $this->pagination->create_links();

        $this->data['listImage'] = $this->Mimage->getImageByLimit($categoryPageConfig['per_page'],$curOffset);

        $this->ci_smarty->view('backend/list-image.tpl',$this->data); 
    }

    public function updateImage($image_id)
    {
        $this->data['image'] = $this->Mimage->getImageByID($image_id); 
        
        if($this->input->post()){
            $image = $this->Mimage->uploadImage();
            $data = $this->input->post();
            if($image['is_image'] == 1)
            {
                $data['filename'] = $image['file_name'];
            }
            $this->Mimage->updateImage($data); 
            redirect(base_url()."admin/list-image.html"); 
        }
       
        $this->ci_smarty->view('backend/update-image.tpl',$this->data); 
    }

    public function deleteImage($image_id)
    {
        $this->Mimage->deteleImage($image_id); 
        redirect(base_url()."admin/list-image.html"); 
    }
}